package com.gjzhao.fmmall.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.gjzhao.fmmall.dao.ShoppingCartMapper;
import com.gjzhao.fmmall.entity.ShoppingCart;
import com.gjzhao.fmmall.service.ShoppingCartService;
import com.gjzhao.fmmall.vo.ResponseResultVO;
import com.gjzhao.fmmall.vo.RespCode;
import com.gjzhao.fmmall.vo.ShoppingCartVO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @Description: 购物车 ServiceImpl
 * @Author: gjzhao
 * @Date Created in 2021-12-31 11:13 下午
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;


    /**
     * 查询购物车列表
     *
     * @param userId
     * @return
     */
    //@Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResponseResultVO listShoppingCartsByUserId(int userId) {

        if (StrUtil.isBlank(String.valueOf(userId))) {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "请输入用户id信息", null);
        }

        List<ShoppingCartVO> shoppingCartList = shoppingCartMapper.selectShoppingCartByUserId(userId);
        return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", shoppingCartList);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ResponseResultVO addShoppingCart(ShoppingCart shoppingCart) {
        //添加购物车时间
        shoppingCart.setCartTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
        int result = shoppingCartMapper.insert(shoppingCart);
        if (result > 0) {
            return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", shoppingCart);
        } else {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "fail", null);
        }
    }

    /**
     * 修改购物车数量
     *
     * @param cartId
     * @param cartProductNum
     * @return
     */
    @Override
    public ResponseResultVO updateShoppingCartNum(String cartId, String cartProductNum) {

        if (StrUtil.isBlank(cartId)) {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "请携带购物车id", null);
        }

        int result = shoppingCartMapper.updateShoppingCartNumByCartId(Integer.parseInt(cartId), cartProductNum);

        /* 可直接使用tkmapper提供的方法进行部分字段更新
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setCartId(Integer.parseInt(cartId));
            shoppingCart.setCartNum(cartProductNum);
            int updateResult = shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
         */

        if (result > 0) {
            return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", null);
        } else {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "fail", null);
        }
    }

    /**
     * 获取一组购物车信息
     *
     * @param cartIds
     * @return
     */
    @Override
    public ResponseResultVO listShoppingCartByCartIds(String cartIds) {

        if (StrUtil.isBlank(cartIds)) {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "购物车id为空", null);
        }

        String[] ids = cartIds.split(",");
        List<Integer> listIds = new ArrayList<>();
        for (String id : ids) {
            listIds.add(Integer.parseInt(id));
        }

        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.listShoppingCartByCartIds(listIds);
        return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", shoppingCartVOS);
    }

    /**
     * 获取一组购物车信息-使用tkmapper实现
     * - 使用 tkmapper 只能查询到某张表中拥有的字段，因此没法查询到商品名称、图片、单价等信息
     *
     * @param cartIds
     * @return
     */
    @Override
    public ResponseResultVO listShoppingCartByCartIdsByMapper(String cartIds) {

        if (StrUtil.isBlank(cartIds)) {
            return new ResponseResultVO(RespCode.FAILED.getCode(), "购物车id为空", null);
        }

        String[] ids = cartIds.split(",");
        List<Integer> listIds = new ArrayList<>();
        for (String id : ids) {
            listIds.add(Integer.parseInt(id));
        }

        Example example = new Example(ShoppingCart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("cartId", listIds);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByExample(example);

        return new ResponseResultVO(RespCode.SUCCESS.getCode(), "success", shoppingCarts);
    }
}
